﻿using HAF.LogUtility;
using HAF.LogUtility.Assistantor;
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using TreeViewDev.Model;

namespace TreeViewDev
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string[] sites = { "金牛区", "金牛区指挥所", "金牛区指挥部", "金牛区酒店", "金牛区酒店指挥所", "青羊区指挥部", "青羊区指挥所", "青羊区酒店指挥所", "青羊区男厕", "青羊区商城", "青羊区B", "青羊区蓝湾酒店" };

        private void button1_Click(object sender, EventArgs e)
        {
            string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "SitesType.json");
            LogHelper.WriteDebugLog(string.Format("读取本地配置文件：{0}", cfgPath));
            if (File.Exists(cfgPath))
            {
                TreeSiteTypes _cfgServices = JsonAssistant.FromFile<TreeSiteTypes>(cfgPath);
                BindTreeView(_cfgServices);
            }
        }

        private void BindTreeView(TreeSiteTypes types)
        {
            treeView1.Nodes.Clear();
            foreach (var item in types.SiteTypes)
            {
                //动态添加多个根Node
                TreeNode tmpNode = new TreeNode();
                tmpNode.Name = item.Name;
                tmpNode.Text = item.Name;
                tmpNode.Tag = "0";

                string[] queryStrs = item.Query.Replace("，", ",").Split(',');
                if (queryStrs.Length > 0)
                {
                    foreach (var item1 in sites)
                    {
                        if (item1.IndexOf(queryStrs[0]) > 1)
                        {
                            tmpNode.Nodes.Add(item1);
                        }
                    }
                }

                treeView1.Nodes.Add(tmpNode);
                //Console.WriteLine(item.Name);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LogHelper.CleanLogs(6);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            treeView1.Focus();
            foreach (TreeNode item in treeView1.Nodes)
            {
                if (item.Nodes.Count > 0)
                {
                    item.Expand();
                    foreach (TreeNode itemsub in item.Nodes)
                    {
                        treeView1.SelectedNode = itemsub;
                        Thread.Sleep(1000);
                    }
                    Thread.Sleep(1000);
                }
                
            }
        }

        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                //将上一个选中的节点背景色还原（原先没有颜色）
                treeView1.SelectedNode.BackColor = Color.Empty;
                //还原前景色
                treeView1.SelectedNode.ForeColor = Color.Black;
            }
        }

        private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds);
            if (e.State == TreeNodeStates.Selected)//做判断
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, new Rectangle(e.Node.Bounds.Left, e.Node.Bounds.Top, e.Node.Bounds.Width, e.Node.Bounds.Height));//背景色为蓝色
                RectangleF drawRect = new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width + 10, e.Bounds.Height);
                e.Graphics.DrawString(e.Node.Text, Font, Brushes.White, drawRect);//字体为白色
            }
            else
            {
                e.DrawDefault = true;
            }
        }
    }
}
